//
//  LDDataPickerView.m
//  zhilian
//
//  Created by 李丹 on 2019/7/23.
//  Copyright © 2019 LZT. All rights reserved.
//

#import "LDDataPickerView.h"
@interface LDDataPickerView ()
@property (nonatomic, strong)LDBottomPopView *popView;
@end
@implementation LDDataPickerView

- (void)setSelectString:(NSString *)selectString{
    _selectString = selectString;
    if (selectString==nil) {
        
    }else{
        if ([self.dataArray containsObject:selectString]) {
            NSInteger index = [self.dataArray indexOfObject:selectString];
            if (index< self.dataArray.count) {
                [self.pickerView selectRow:index inComponent:0 animated:NO];
                [self pickerView:self.pickerView didSelectRow:index inComponent:0];
                
            }
            
        }
    }
}
-(instancetype)initWithArray:(NSArray *)arr {
    if (self = [super initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, RatioHeight(121))]) {
        
        self.dataArray = arr;
        CGFloat rowHeight = RatioHeight(30);
        CGFloat width = [arr.firstObject getTextWidth:rowHeight font:TGAutoFont(14)];
        _pickerView = [[PGPickerView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - MAX(130, width)) / 2, 0, MAX(130, width), rowHeight*3)];
        [self addSubview:_pickerView];
        self.pickerView.delegate = self;
        self.pickerView.dataSource = self;
//        self.pickerView.type = PGPickerViewLineTypelineSegment;
        //        [self.pickerView selectRow:7 inComponent:1 animated:true];
        self.pickerView.rowHeight = rowHeight;
        self.pickerView.isHiddenMiddleText = false;
        self.pickerView.textFontOfOtherRow = TGAutoFont(14);
        self.pickerView.textFontOfSelectedRow = TGAutoFont(14);
        
        //设置线条的颜色
        self.pickerView.lineBackgroundColor = RGB(231,231,231);
        //设置选中行的字体颜色
        self.pickerView.textColorOfSelectedRow = TitleBlackColor;
        //设置未选中行的字体颜色
        self.pickerView.textColorOfOtherRow = RGBA(204,204,204,1);
        self.pickerView.lineHeight = PixelOne;
        [self.pickerView selectRow:0 inComponent:0 animated:true];
        
        _pickerView.centerY = self.centerY;
    }
    return self;
}
- (void)setMaxPickerWidth:(CGFloat)maxPickerWidth {
    _maxPickerWidth = maxPickerWidth;
    self.pickerView.width = _maxPickerWidth;
    self.pickerView.centerX = self.centerX;
}
#pragma UIPickerViewDataSource
- (NSInteger)numberOfComponentsInPickerView:(PGPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(PGPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.dataArray.count;
}
#pragma UIPickerViewDelegate
- (nullable NSString *)pickerView:(PGPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return self.dataArray[row];
}

- (void)pickerView:(PGPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"row = %ld component = %ld", row, component);
    self.selectRow = row;
   
}

- (void)configurePickerWithBgColor:(UIColor *)bgColor
               actionBtnTitleColor:(UIColor *)titleColor
                       centerTitle:(NSString *)centerTitle {
    [self.popView.backView setBackgroundColor:bgColor];
    self.popView.backView.layer.cornerRadius = 12;
    self.popView.backView.layer.qmui_maskedCorners = QMUILayerMinXMinYCorner|
    QMUILayerMaxXMinYCorner;
    [self.popView.leftBtn setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [self.popView.rightBtn setTitleColor:UIColorWhite forState:UIControlStateNormal];
    self.popView.centerTitle = centerTitle;
    self.popView.lineView.hidden = YES;
//    self.pickerView.lineBackgroundColor = RGBA(231,231,231, 0.5);
    self.pickerView.lineBackgroundColor = UIColorClear;
    self.pickerView.textColorOfSelectedRow = titleColor;
    self.pickerView.textFontOfSelectedRow = TGAutoFont(16);
    [self setMaxPickerWidth:SCREEN_WIDTH];
}

- (void)show{
    [self.popView show];
}
- (void)sureAction:(id)sender{
    if (self.selectedHandler) {
        self.selectedHandler(self.dataArray[self.selectRow]);
    }
    if (self.selectedAllBock) {
        self.selectedAllBock(self.dataArray[self.selectRow], self.selectRow);
    }
    [_popView hide];
    
}

- (LDBottomPopView *)popView {
    if (!_popView) {
        _popView = [[LDBottomPopView alloc] initWithAlertView:self];
        [_popView addTagert:self selector:@selector(sureAction:)];
        WEAK
        _popView.hideblock = ^{
            STRONG
            if (self.hideblock) {
                self.hideblock();
            }
        };
    }
    return _popView;
}

@end
